<?php
namespace app\admin\controller;
use think\Db;
use think\Session;
use think\Request;

class Node extends Admincontroller
{
	/**
	 * 后台节点列表加载
	 * @param  Request $req [description]
	 * @return [type]       [description]
	 */
	public function index(Request $req)
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		// 查询节点表数据
		$list = Db::name('node')->select();

		return $this->fetch('list',['title'=>'蒸汽男孩后台','nick'=>$name['name'],'list'=>$list]);
	}

	/**
	 * 加载节点增加页面
	 */
	public function add()
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		return $this->fetch('add',['title'=>'蒸汽男孩后台','nick'=>$name['name']]);
	}

	/**
	 * 执行新增节点逻辑
	 * @return [type] [description]
	 */
	public function insert()
	{
		$name = $_POST['name'];
		$mname = $_POST['mname'];
		$aname = $_POST['aname'];
		if($name == '' || $mname == '' || $aname == '') {
			$this->error('你似乎少填了什么?');
		}
		$data = Db::name('node')->insert(['name'=>$name,'mname'=>$mname,'aname'=>$aname]);

		if($data > 0) {
			$this->success('添加成功',url('admin/node/index'));
		} else {
			$this->error('添加失败,请重试');
		}

	}

	/**
	 * 执行删除节点逻辑
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function delete($id)
	{
		$result = Db::name('node')->delete($id);
		if( $result>0 ) {
			$this->success('删除成功',url('admin/node/index'));
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 加载节点编辑页面
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function edit($id)
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		$data = Db::name('node')->find($id);
		return $this->fetch('edit',['title'=>'蒸汽男孩后台','nick'=>$name['name'],'data'=>$data]);
	}

	/**
	 * 执行节点更新逻辑
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function update($id)
	{

		$name = $_POST['name'];
		$mname = $_POST['mname'];
		$aname = $_POST['aname'];
		if($name == '' || $mname == '' || $aname == '') {
			$this->error('你似乎少填了什么?');
		}
		$result = Db::name('node')->where('id = "'.$id.'"')->update(['name'=>$name,'mname'=>$mname,'aname'=>$aname]);
		if($result > 0) {
			$this->success('修改成功',url('admin/node/index'));
		} else {
			$this->error('修改失败,请重试');
		}
	}
}